class EventCenterHandler {
  listener = null;
  target = null;
  event = null;
  handler = null;
  constructor(e, t, n, r) {
    this.listener = t;
    this.target = e;
    this.event = n;
    this.handler = r;
  }
  execute(e) {
    let t = this;
    t.handler.call(t.listener, e);
  }
  checkIfMatch(e, t, n, r) {
    let i = this,
      s = !0;
    typeof e != "undefined" && e != null && (s = i.target === e),
      s && typeof t != "undefined" && t != null && (s = i.listener === t);
    if (s && typeof n != "undefined") {
      let o = !1;
      for (let u = 0; u < n.length; u++) {
        let a = n[u];
        if (a == i.event) {
          o = !0;
          break;
        }
      }
      s = o;
    }
    if (s && typeof r != "undefined") {
      let f = !1;
      for (let l = 0; l < r.length; l++) {
        let c = r[l];
        if (c === i.handler) {
          f = !0;
          break;
        }
      }
      s = f;
    }
    return s;
  }
  release() {
    let e = this;
    e.listener = null;
    e.target = null;
    e.event = null;
    e.handler = null;
  }
}

class EventCenter {
  static listeners = [];
  static addListener(t, r, i, s) {
    (i = typeof i == "string" ? [i] : i),
      (s = typeof s == "function" ? [s] : s);
    for (let o = 0; o < i.length && o < s.length; o++) {
      let u = i[o],
        a = s[o],
        f = new EventCenterHandler(t, r, u, a);
      this.listeners.push(f);
    }
  }
  static removeListener(e, t?, r?, i?) {
    (r = typeof r == "string" ? [r] : r),
      (i = typeof i == "function" ? [i] : i);
    for (let s = this.listeners.length - 1; s >= 0; s--) {
      let o = this.listeners[s];
      o.checkIfMatch(e, t, r, i) && this.listeners.splice(s, 1);
    }
  }
  static dispatchEvent(e) {
    let t = e.type,
      r = e.target,
      i = this.listeners.slice();
    for (let s = 0; s < i.length; s++) {
      let o = i[s];
      o != null &&
        o != undefined &&
        o.checkIfMatch(r, null, [t]) &&
        o.execute(e);
    }
  }
}

export default EventCenter;
